package com.cn.algorithm.two.union;

public class UnionFind_QU_R extends UnionFind_QU{

    int[] rank;

    public UnionFind_QU_R(int capacity) {
        super(capacity);
        rank = new int[capacity];
        for (int i = 0; i < rank.length; i++) {
            rank[i] = 1;
        }
    }

    @Override
    void union(int v1, int v2) {
        int k1 = find(v1);
        int k2 = find(v2);
        if (k1 == k2) {
            return;
        }
        if (rank[k1] > rank[k2]) {
            parent[k2] = k1;
        } else if (rank[k1] < rank[k2]) {
            parent[k1] = k2;
        } else {
            parent[k1] = k2;
            rank[k2]++;
        }
    }
}
